package com.company.project.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.project.common.job.CorporOV;
import com.company.project.common.job.OvsAndMiddlenum;
import com.company.project.common.job.task.CallBackTask;
import com.company.project.common.redismq.ZSetVer;
import com.company.project.common.utils.Func;
import com.company.project.common.utils.StringUtil;
import com.company.project.entity.*;
import com.company.project.mapper.*;
import com.company.project.service.TCallbakTaskService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service("tCallbakTaskService")
public class TCallbakTaskServiceImpl extends ServiceImpl<TCallbakTaskMapper, TCallbakTaskEntity> implements TCallbakTaskService {

    @Autowired
    public TSeatsMapper tSeatsMapper;
    @Autowired
    public TXiaohaoMapper xiaohaoMapper;
    @Resource
    public TCallbakTaskMapper tCallbakTaskMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    @Autowired
    private TCallbakMapper callbakMapper;
    @Autowired
    private TCallbakLogMapper callbakLogMapper;
    @Autowired
    private TThjlmxMapper thjlmxMapper;
    @Autowired
    private THwtjFzsMapper tHwtjFzsMapper;

    @Autowired
    public TThrbMapper thrbMapper;
    @Autowired
    private THwtjMapper hwtjMapper;

    @Autowired
    private TMiddlenumMapper tMiddlenumMapper;
    @Autowired
    private TXiaohaoMapper tXiaohaoMapper;
    @Autowired
    private TWhbcsMapper tWhbcsMapper;
    @Autowired
    public TBacklistMapper backlistMapper;
    @Autowired
    private ZSetVer zSetVer;
    private Logger logger = LoggerFactory.getLogger(getClass());


    public void testConsumerDelayMessage2() {
        String id = "id";
        zSetVer.consumerDelayMessage(id);
    }

    @Override
    public Map<String, Object> notifyurl1(CallbackEntityVo1 vo) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> mapFzsGs = new HashMap<>();
        try {
            Date date = new Date();
            TCallbakLogEntity tCallbakLog = new TCallbakLogEntity();
            tCallbakLog.setBindId(vo.getSessionId());//绑定 ID
            tCallbakLog.setZjh(vo.getBindNbr());//中间号
            tCallbakLog.setZjhm(vo.getCallerNbr());//主叫号码
            tCallbakLog.setBjhm(vo.getCalleeNbr());//被叫号码
            tCallbakLog.setStartTime(vo.getStartTime());
            tCallbakLog.setEndTime(vo.getEndTime());
            tCallbakLog.setCallOutStartTime(vo.getCallOutStartTime());
            tCallbakLog.setDuration(vo.getDuration());
            tCallbakLog.setRecordUrl(vo.getRecordUrl());//录音地址
            tCallbakLog.setStatus(vo.getStatus());
            tCallbakLog.setUlFailReason(vo.getUlFailReason());
            tCallbakLog.setAddtime(date);

            String jsonString = JSON.toJSONString(tCallbakLog);
            zSetVer.producer("tCallbakLog", jsonString);

            long start = System.currentTimeMillis();
            //2次 回调不记录
            TCallbakEntity callbakUser = callbakMapper.selectOneBySessionId(vo.getSessionId());
            if (callbakUser == null) {
                return null;
            }
            long end = System.currentTimeMillis();
            log.info("TCallbakEntity总耗时：{} ms", end - start);
            //  log.info("Callbak --notifyurl1 callbakUser  参数: " + callbakUser.toString());

            start = System.currentTimeMillis();
            System.out.println("callbakUser.getUserid():"+callbakUser.getUserid());
            //通话人
            TSeatsEntity tSeatsEntity = tSeatsMapper.selectById(callbakUser.getUserid());
            end = System.currentTimeMillis();
            log.info("通话人总耗时：{} ms", end - start);
            if (tSeatsEntity == null) return null;
            log.info("tSeatsEntity：{} ms",tSeatsEntity.toString());
            SysUser sysUser = sysUserMapper.selectById(tSeatsEntity.getAccountid());
            start = System.currentTimeMillis();
            end = System.currentTimeMillis();
            log.info("SysUser总耗时：{} ms", end - start);
            //  log.info("Callbak --notifyurl1 sysUser参数: " + sysUser.toString());
            if (sysUser == null) {
                sysUser = new SysUser();
                sysUser.setUsername("admin");
            }

            String line = tSeatsEntity.getLine();
            String ulFailReason = vo.getUlFailReason();
            if (Func.isEmpty(ulFailReason)) {
                if (Func.equals("有销移动", line)) {
                    ulFailReason = "yx未知";
                }
            }

            start = System.currentTimeMillis();
            List<TThjlmxEntity> ttList = thjlmxMapper.selectList(Wrappers.<TThjlmxEntity>lambdaQuery()
                    .eq(TThjlmxEntity::getBindId, vo.getSessionId()));
            end = System.currentTimeMillis();
            log.info("thjlmxMapper.selectList总耗时：{} ms", end - start);
            start = System.currentTimeMillis();
            Long tThrbCount = thrbMapper.selectCount(Wrappers.<TThrbEntity>lambdaQuery()
                    .eq(TThrbEntity::getBindId, vo.getSessionId()));
            end = System.currentTimeMillis();
            log.info("thrbMapper.selectCount  总耗时：{} ms", end - start);

            start = System.currentTimeMillis();
            Long tHwtjCount = hwtjMapper.selectCount(Wrappers.<THwtjEntity>lambdaQuery()
                    .eq(THwtjEntity::getBindId, vo.getSessionId()));
            end = System.currentTimeMillis();
            log.info("hwtjMapper.selectCount  总耗时：{} ms", end - start);

            TThjlmxEntity tt = new TThjlmxEntity();
            if (!ttList.isEmpty()) {
                tt = ttList.get(0);
            }

            //TODO 暂时不用过滤这段代码
//            if (tThrbCount > 0 || tHwtjCount > 0) {
//                log.info("通话日报已存在:{}", vo.getSessionId());
//                log.info("话务统计已存在:{}", vo.getSessionId());
//                //记录通话明细
//                if (Func.isNotEmpty(vo.getRecordUrl()) && tt.getId() != null) {
//                    tt.setLy(vo.getRecordUrl());//录音
//                    map.put("thjlmx", tt);
//                    jsonString = JSON.toJSONString(tt);
//                    zSetVer.producer("thjlmx", jsonString);
//                    return map;
//                }
//                return map;
//            }
//            if (tCallbakLog.getBjhm() == null) {
//                //记录通话明细
//                if (Func.isNotEmpty(vo.getRecordUrl())) {
//                    tt.setLy(vo.getRecordUrl());//录音
//                    map.put("thjlmx", tt);
//                    jsonString = JSON.toJSONString(tt);
//                    zSetVer.producer("thjlmx", jsonString);
//                }
//                return map;
//            }
            String thsc = "0";
            long seconds = 0;
            long minutes = 0;
            String jts = "0";//接通数
            if (Func.equals("1", vo.getStatus())) {
                //通话时长  seconds 不足一分钟按一分钟计算
                thsc = String.valueOf(DateUtil.between(vo.getStartTime(), vo.getEndTime(), DateUnit.SECOND));
                seconds = DateUtil.between(vo.getStartTime(), vo.getEndTime(), DateUnit.SECOND);
                minutes = seconds / 60;
                seconds -= minutes * 60;
                //计费分钟数 minutes
                if (seconds > 0) {
                    minutes++;
                }
                jts = "1";
            }

            if (tt != null) {
                //记录通话明细
                if (Func.isNotEmpty(vo.getRecordUrl())) tt.setLy(vo.getRecordUrl());//录音
                if (sysUser != null && Func.isNotEmpty(sysUser.getUsername())) tt.setYgxm(sysUser.getUsername());//员工姓名
                if (Func.isNotEmpty(vo.getCallerNbr())) tt.setZjhm(vo.getCallerNbr());
                if (Func.isNotEmpty(vo.getBindNbr())) tt.setXh(vo.getBindNbr());//小号
                if (Func.isNotEmpty(vo.getCalleeNbr())) tt.setBjhm(vo.getCalleeNbr());//被叫号码
                if ( vo.getRecordUrl() != null) {
                    tt.setStart("1");//状态
                } else {
                    if (Func.isNotEmpty(vo.getStatus())) tt.setStart(vo.getStatus());//状态
                }
                if (Func.isNotEmpty(ulFailReason)) tt.setStartinfo(ulFailReason);//状态明细
                if (Func.isNotEmpty(vo.getCallOutStartTime())) tt.setZjjttime(vo.getCallOutStartTime());//主叫接通时间
                if (Func.isNotEmpty(vo.getStartTime())) tt.setBjjttime(vo.getStartTime());//被叫接通时间
                if (Func.isNotEmpty(vo.getEndTime())) tt.setEndtime(vo.getEndTime());//结束时间
                if (Func.isNotEmpty(thsc) && Func.isNotEmpty(vo.getEndTime())) tt.setThsc(thsc);//通话时长
                if (Func.isNotEmpty(tSeatsEntity.getDept())) tt.setOrg(tSeatsEntity.getDept());//所属部门
//            thjlmxMapper.updateById(tt);
                map.put("thjlmx", tt);
                jsonString = JSON.toJSONString(tt);
                log.info("thjlmx 内容：{} ms", jsonString.toString());

                zSetVer.producer("thjlmx", jsonString);
                //  log.info("记录通话明细:{}", tt.toString());
            }
            start = System.currentTimeMillis();
            //公司主坐席
            TSeatsEntity gsgl = tSeatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery()
                    .eq(TSeatsEntity::getCorporatename, tSeatsEntity.getCorporatename()
                    ).eq(TSeatsEntity::getGsgl, "1"));
            end = System.currentTimeMillis();
            log.info("公司主坐席总耗时：{} ms", end - start);
            Integer xhfzs = Integer.valueOf(gsgl.getResidue()) - (int) minutes;
            //  log.info("{}扣除分钟数为:{}", tSeatsEntity.getCorporatename(), minutes);

            //存储需要扣除分钟数和公司名称
            THwtjFzsEntity ov = new THwtjFzsEntity();
            ov.setCompany(tSeatsEntity.getCorporatename());
            ov.setKcfzs((int) minutes);

            jsonString = JSON.toJSONString(ov);
            zSetVer.producer("hwtjFzs", jsonString);

            //通话日报 t_thrb
            TThrbEntity thrb = new TThrbEntity();
            thrb.setUsername(sysUser.getUsername());
            thrb.setOrg(tSeatsEntity.getDept());
            thrb.setCompany(tSeatsEntity.getCorporatename());
            thrb.setJts(jts);
            String bdcs = "1";//拨打次数
            thrb.setBdcs(bdcs);
            thrb.setThsc(thsc);//通话时长
            thrb.setJfsc(String.valueOf(minutes));//计费时长
            thrb.setThtime(date);//通话时间
            thrb.setXs(tSeatsEntity.getSalesname());//销售
            thrb.setBindId(vo.getSessionId());//回调唯一id
            map.put("thrb", thrb);

            jsonString = JSON.toJSONString(thrb);
            zSetVer.producer("thrb", jsonString);

            start = System.currentTimeMillis();
            //话务统计 t_hwtj 获取公司的 的分钟数
            THwtjEntity hwtj = hwtjMapper.selectOneByCorporatename(tSeatsEntity.getCorporatename());
            end = System.currentTimeMillis();
            log.info("话务统计 t_hwtj总耗时：{} ms", end - start);
            Date nowData = date;
            if (nowData.equals(hwtj.getDate())) {
                long timeInMillis = nowData.getTime(); // 转换为毫秒数
                timeInMillis += 1000; // 加1秒（1000毫秒）
                nowData.setTime(timeInMillis); // 更新时间
            }
            double random = Math.random() * 100;
            String sessionId = vo.getSessionId();
            hwtj.setId(sessionId);
            hwtj.setCompany(tSeatsEntity.getCorporatename());//公司名称
            hwtj.setSyfzs(gsgl.getResidue());
            hwtj.setBscs(bdcs);
            hwtj.setJts(jts);
            hwtj.setJfsc(String.valueOf(minutes));//计费时长
            hwtj.setThsc(thsc);//通话时长
            hwtj.setXs(tSeatsEntity.getSalesname());//销售
            hwtj.setOrgname(tSeatsEntity.getDept());//所属部门
            hwtj.setLine(tSeatsEntity.getLine());//线路
            hwtj.setDate(nowData);
            hwtj.setBindId(vo.getSessionId());//回调唯一id
            String createuser = gsgl.getCreateuser();
            if (!Func.equals("guest", createuser)) {//非管理员创建 就是代理商
                hwtj.setDl(createuser);//代理商
            }
            map.put("hwtj", hwtj);
            jsonString = JSON.toJSONString(hwtj);
            zSetVer.producer("hwtj", jsonString);
//        hwtjMapper.insert(hwtj);
            //  log.info("话务统计:{}", hwtj.toString());

            if (!Func.equals(vo.getBindNbr(), vo.getCallerNbr())) {
                //  log.info("中间号:{}", vo.getBindNbr());
                //  log.info("主叫号码:{}", vo.getCallerNbr());
                //中间号  减去剩余分钟数
                TMiddlenumEntity tMiddlenumEntity = tMiddlenumMapper.selectOne(Wrappers.<TMiddlenumEntity>lambdaQuery()
                        .eq(TMiddlenumEntity::getMiddlenum, vo.getBindNbr()));
                if (tMiddlenumEntity != null) {
                    map.put("tMiddlenumEntity", tMiddlenumEntity);
                    jsonString = JSON.toJSONString(tMiddlenumEntity);
                    zSetVer.producer("tMiddlenum", jsonString);
//                    //  log.info("中间号实体:{}", tMiddlenumEntity.toString());
//                    tMiddlenumEntity.setHjcs(tMiddlenumEntity.getHjcs() + 1);
//                    tMiddlenumEntity.setSycs(tMiddlenumEntity.getSycs() - 1);
//                    //  log.info("中间号实体修改后:{}", tMiddlenumEntity.toString());
//                    tMiddlenumMapper.updateById(tMiddlenumEntity);
//
//                    List<TXiaohaoEntity> tXiaohaoEntitys = tXiaohaoMapper.selectList(Wrappers.<TXiaohaoEntity>lambdaQuery()
//                                    .eq(TXiaohaoEntity::getXiaohao, vo.getBindNbr())
////                .eq(TXiaohaoEntity::getAccountid,gsgl.getAccountid())
//                    );
//                    for (TXiaohaoEntity t : tXiaohaoEntitys) {
//                        t.setShengyucishu(tMiddlenumEntity.getSycs());
//                        tXiaohaoMapper.updateById(t);
//                        //  log.info("小号  减去剩余分钟数:{}", t.toString());
//                    }
//                    //  log.info("中间号  减去剩余分钟数:{}", tMiddlenumEntity.toString());
                }

            }

            //记录未接通数据
            if (Func.equals("2", vo.getStatus())) {
                TWhbcsEntity tWhbcs = new TWhbcsEntity();
                tWhbcs.setXh(vo.getBindNbr());
                tWhbcs.setDate(date);
                tWhbcs.setWhbcs(1);
                map.put("tWhbcs", tWhbcs);
                jsonString = JSON.toJSONString(tWhbcs);
                zSetVer.producer("tWhbcs", jsonString);
//                tWhbcsMapper.insert(tWhbcs);
            }
            //  log.info("记录结束");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public void runCallBack(List<TCallbakTaskEntity> tCallbakTaskEntities) {
        List<String> sessionIds = new ArrayList<>();
        List<String> bakIds = new ArrayList<>();
        List<String> corporatenames = new ArrayList<>();
        List<CorporOV> ovs = new ArrayList<>();
        List<TCallbakLogEntity> callLogList = new ArrayList<>();
        List<TThrbEntity> thrbList = new ArrayList<>();
        List<THwtjEntity> hwtjList = new ArrayList<>();
        List<TWhbcsEntity> whbcsList = new ArrayList<>();
        List<TThjlmxEntity> thjlmxList = new ArrayList<>();
        List<TMiddlenumEntity> middlenumEntityList = new ArrayList<>();
        List<String> middlenums = new ArrayList<>();
        //记入日志
//        callbakLogMapper.insertByCallList(tCallbakTaskEntities);
//        //删除需要计算的数据
//        tCallbakTaskMapper.deleteByList(tCallbakTaskEntities);

        for (TCallbakTaskEntity task : tCallbakTaskEntities) {
            CallbackEntityVo1 vo = new CallbackEntityVo1();
            vo.setSessionId(task.getBindId());//绑定 ID
            vo.setBindNbr(task.getZjh());//中间号
            vo.setCallerNbr(task.getZjhm());//主叫号码
            vo.setCalleeNbr(task.getBjhm());//被叫号码
            vo.setStartTime(task.getStartTime());
            vo.setEndTime(task.getEndTime());
            vo.setCallOutStartTime(task.getCallOutStartTime());
            vo.setDuration(task.getDuration());
            vo.setRecordUrl(task.getRecordUrl());//录音地址
            vo.setStatus(task.getStatus());
            vo.setUlFailReason(task.getUlFailReason());

            try {
                long start = System.currentTimeMillis();
                //开始计算
                Map<String, Object> map = notifyurl1(vo);
                long end = System.currentTimeMillis();
                log.info("notifyurl1总耗时：{} ms", end - start);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (1==1) return;
        for (TCallbakTaskEntity task : tCallbakTaskEntities) {
            CallbackEntityVo1 vo = new CallbackEntityVo1();
            vo.setSessionId(task.getBindId());//绑定 ID
            vo.setBindNbr(task.getZjh());//中间号
            vo.setCallerNbr(task.getZjhm());//主叫号码
            vo.setCalleeNbr(task.getBjhm());//被叫号码
            vo.setStartTime(task.getStartTime());
            vo.setEndTime(task.getEndTime());
            vo.setCallOutStartTime(task.getCallOutStartTime());
            vo.setDuration(task.getDuration());
            vo.setRecordUrl(task.getRecordUrl());//录音地址
            vo.setStatus(task.getStatus());
            vo.setUlFailReason(task.getUlFailReason());

            try {
                long start = System.currentTimeMillis();
                //开始计算
                Map<String, Object> map = notifyurl1(vo);
                long end = System.currentTimeMillis();
                log.info("notifyurl1总耗时：{} ms", end - start);
                if(map != null || map == null) continue; //TODO 暂时退出循环
                if(map == null) continue;//为了不出异常加个判断
                logger.info("notifyurl1--map: {}", map.toString());

                TCallbakLogEntity tCallbakLog = (TCallbakLogEntity) map.get("tCallbakLog");
                if (tCallbakLog != null) callLogList.add((TCallbakLogEntity) map.get("tCallbakLog"));
                TThrbEntity tThrb = (TThrbEntity) map.get("thrb");
                if (tThrb != null) thrbList.add((TThrbEntity) map.get("thrb"));
                THwtjEntity hwtj = (THwtjEntity) map.get("hwtj");
                if (hwtj != null) {
                    THwtjEntity hwtjNew = new THwtjEntity();
                    hwtjNew.setId(task.getBindId());
                    hwtjNew.setCompany(hwtj.getCompany());//公司名称
                    hwtjNew.setSyfzs(hwtj.getSyfzs());
                    hwtjNew.setFzsze(hwtj.getFzsze());
                    hwtjNew.setBscs(hwtj.getBscs());
                    hwtjNew.setJts(hwtj.getJts());
                    hwtjNew.setJfsc(hwtj.getJfsc());//计费时长
                    hwtjNew.setThsc(hwtj.getThsc());//通话时长
                    hwtjNew.setXs(hwtj.getXs());//销售
                    hwtjNew.setOrgname(hwtj.getOrgname());//所属部门
                    hwtjNew.setLine(hwtj.getLine());//线路
                    hwtjNew.setDate(hwtj.getDate());
                    hwtjNew.setBindId(task.getBindId());//回调唯一id
                    hwtjNew.setDl(hwtj.getDl());//代理商
                    hwtjList.add(hwtjNew);
                }

                start = System.currentTimeMillis();

                TWhbcsEntity tWhbcs = (TWhbcsEntity) map.get("tWhbcs");
                if (tWhbcs != null) whbcsList.add((TWhbcsEntity) map.get("tWhbcs"));
                thjlmxList.add((TThjlmxEntity) map.get("thjlmx"));

                sessionIds.add(vo.getSessionId());
                bakIds.add(task.getId());
                if (map.get("minutes") != null) {
                    String s = String.valueOf(map.get("corporatename"));
                    //是否已存在这个公司数据
                    boolean contains = corporatenames.contains(s);
                    int integer = Integer.valueOf(String.valueOf(map.get("minutes")));
                    logger.info("{}扣除分钟数为:{}", s, integer);
                    if (contains) {
                        for (CorporOV ov : ovs) {
                            //有相同的公司统计分钟数
                            if (StringUtil.equals(ov.getCorporatename(), s)) {
                                int minutes = ov.getMinutes();
                                ov.setMinutes(minutes + integer);
                            }
                        }
                    } else {
                        CorporOV corporOV = new CorporOV();
                        corporOV.setCorporatename(s);
                        corporOV.setMinutes(integer);

                        ovs.add(corporOV);
                        corporatenames.add(s);
                    }
                }
                //中间号处理
                if (map.get("tMiddlenumEntity") != null) {
                    TMiddlenumEntity tMiddlenumEntity = (TMiddlenumEntity) map.get("tMiddlenumEntity");
                    //是否已存在这个公司数据
                    boolean contains = middlenums.contains(tMiddlenumEntity.getMiddlenum());
                    if (contains) {
                        for (TMiddlenumEntity ov : middlenumEntityList) {
                            //有相同的公司统计分钟数
                            if (StringUtil.equals(ov.getMiddlenum(), tMiddlenumEntity.getMiddlenum())) {
                                int minutes = ov.getSycs();
                                ov.setSycs(minutes + 1);
                            }
                        }

                    } else {
                        TMiddlenumEntity o = new TMiddlenumEntity();
                        o.setMiddlenum(tMiddlenumEntity.getMiddlenum());
                        o.setSycs(1);
                        middlenums.add(tMiddlenumEntity.getMiddlenum());
                        middlenumEntityList.add(o);
                    }
                }
                end = System.currentTimeMillis();
                log.info("中间号处理 总耗时：{} ms", end - start);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            long start = System.currentTimeMillis();
//            if (callLogList.size() > 0) {
//                ArrayList<TCallbakLogEntity> collect = callLogList.stream()
//                        .collect(Collectors.collectingAndThen(Collectors
//                                .toCollection(() -> new TreeSet<>(Comparator
//                                        .comparing(s -> s.getBindId()))), ArrayList::new));
//                collect.forEach(System.out::println);
//                callbakLogMapper.insertList(collect);
//            }
            long end = System.currentTimeMillis();
            //  log.info("callLogList 总耗时：{} ms", end - start);

            start = System.currentTimeMillis();
            if (thrbList.size() > 0) {
                ArrayList<TThrbEntity> collect = thrbList.stream()
                        .collect(Collectors.collectingAndThen(Collectors
                                .toCollection(() -> new TreeSet<>(Comparator
                                        .comparing(s -> s.getBindId()))), ArrayList::new));
                collect
                        .forEach(System.out::println);
                thrbMapper.insertList(collect);
            }
            end = System.currentTimeMillis();
            log.info("thrbList 总耗时：{} ms", end - start);


            start = System.currentTimeMillis();
            if (hwtjList.size() > 0) {
                ArrayList<THwtjEntity> collect = hwtjList.stream()
                        .collect(Collectors.collectingAndThen(Collectors
                                .toCollection(() -> new TreeSet<>(Comparator
                                        .comparing(s -> s.getBindId()))), ArrayList::new));
                collect
                        .forEach(System.out::println);
                hwtjMapper.insertList(collect);
            }
            end = System.currentTimeMillis();
            log.info("hwtjList 总耗时：{} ms", end - start);

            if (whbcsList.size() > 0) tWhbcsMapper.insertList(whbcsList);

            start = System.currentTimeMillis();
            for (CorporOV ov : ovs) {
                //有相同的公司统计分钟数
                //公司主坐席
                TSeatsEntity gsgl = tSeatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery()
                        .eq(TSeatsEntity::getCorporatename, ov.getCorporatename()
                        ).eq(TSeatsEntity::getGsgl, "1"));
                Integer xhfzs = Integer.valueOf(gsgl.getResidue()) - ov.getMinutes();
                tSeatsMapper.updateByCorporatename(ov.getCorporatename(), xhfzs);
            }
            end = System.currentTimeMillis();
            log.info("有相同的公司统计分钟数总耗时：{} ms", end - start);


            long start2 = System.currentTimeMillis();
            for (TMiddlenumEntity ov : middlenumEntityList) {
                String middlenum = ov.getMiddlenum();
                int minutes = ov.getSycs();
                //中间号  减去剩余分钟数
                TMiddlenumEntity tMiddlenumEntity = tMiddlenumMapper.selectOne(Wrappers.<TMiddlenumEntity>lambdaQuery()
                        .eq(TMiddlenumEntity::getMiddlenum, middlenum));
                tMiddlenumEntity.setHjcs(tMiddlenumEntity.getHjcs() + minutes);
                tMiddlenumEntity.setSycs(tMiddlenumEntity.getSycs() - minutes);
                log.info("中间号实体修改后:{}", tMiddlenumEntity.toString());
                tMiddlenumMapper.updateById(tMiddlenumEntity);
                tXiaohaoMapper.updateByMidd(tMiddlenumEntity);
            }
            long end2 = System.currentTimeMillis();
            log.info("中间号实体修改总耗时：{} ms", end2 - start2);

            long start3 = System.currentTimeMillis();
            logger.info("CallBackTask定时任务  thjlmxList.size() :" + thjlmxList.size());
            if (thjlmxList.size() > 0) {
//                ArrayList<TThjlmxEntity> collect = thjlmxList.stream()
//                        .collect(Collectors.collectingAndThen(Collectors
//                                .toCollection(() -> new TreeSet<>(Comparator
//                                        .comparing(s -> s.getBindId()))), ArrayList::new));

//                thjlmxList.forEach(System.out::println);
                thjlmxMapper.deleteListById(thjlmxList);
                thjlmxMapper.insertList(thjlmxList);
            }
            long end3 = System.currentTimeMillis();
            log.info("thjlmxList 总耗时：{} ms", end3 - start3);

            start3 = System.currentTimeMillis();
//            if (sessionIds.size() > 0) {
//                Date calltime = tCallbakTaskEntities.get(0).getAddtime();//呼叫时间
//                tCallbakTaskMapper.deleteHJ(calltime);
//                tCallbakTaskMapper.insertHJ(calltime);
//
//                tCallbakTaskMapper.updateFlag(tCallbakTaskEntities);
//                tCallbakTaskMapper.deleteByFlag(bakIds);
//            }
            end3 = System.currentTimeMillis();
            log.info("tCallbakTaskMapper.deleteByFlag总耗时：{} ms", end3 - start3);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void runCallBackAsync(List<TCallbakTaskEntity> tCallbakTaskEntities) {
        List<String> sessionIds = new ArrayList<>();
        List<String> bakIds = new ArrayList<>();
        List<String> corporatenames = new ArrayList<>();
        List<CorporOV> ovs = new ArrayList<>();
        List<TCallbakLogEntity> callLogList = new ArrayList<>();
        List<TThrbEntity> thrbList = new ArrayList<>();
        List<THwtjEntity> hwtjList = new ArrayList<>();
        List<TWhbcsEntity> whbcsList = new ArrayList<>();
        List<TThjlmxEntity> thjlmxList = new ArrayList<>();
        List<TMiddlenumEntity> middlenumEntityList = new ArrayList<>();
        List<String> middlenums = new ArrayList<>();
        //记入日志
        callbakLogMapper.insertByCallList(tCallbakTaskEntities);
//        //删除需要计算的数据
//        tCallbakTaskMapper.deleteByList(tCallbakTaskEntities);

        for (TCallbakTaskEntity task : tCallbakTaskEntities) {
            CallbackEntityVo1 vo = new CallbackEntityVo1();
            vo.setSessionId(task.getBindId());//绑定 ID
            vo.setBindNbr(task.getZjh());//中间号
            vo.setCallerNbr(task.getZjhm());//主叫号码
            vo.setCalleeNbr(task.getBjhm());//被叫号码
            vo.setStartTime(task.getStartTime());
            vo.setEndTime(task.getEndTime());
            vo.setCallOutStartTime(task.getCallOutStartTime());
            vo.setDuration(task.getDuration());
            vo.setRecordUrl(task.getRecordUrl());//录音地址
            vo.setStatus(task.getStatus());
            vo.setUlFailReason(task.getUlFailReason());

            try {
                long start = System.currentTimeMillis();
                //开始计算
                Map<String, Object> map = notifyurl1(vo);
                long end = System.currentTimeMillis();
                log.info("notifyurl1总耗时：{} ms", end - start);
                if(map == null) continue;//为了不出异常加个判断
                logger.info("notifyurl1--map: {}", map.toString());

                TCallbakLogEntity tCallbakLog = (TCallbakLogEntity) map.get("tCallbakLog");
                if (tCallbakLog != null) {
                    String jsonString = JSON.toJSONString(tCallbakLog);
                    zSetVer.producer("tCallbakLog", jsonString);
//                    callLogList.add((TCallbakLogEntity) map.get("tCallbakLog"));
                }
                TThrbEntity tThrb = (TThrbEntity) map.get("thrb");
                if (tThrb != null) {
                    String jsonString = JSON.toJSONString(tThrb);
                    zSetVer.producer("thrb", jsonString);
//                    thrbList.add((TThrbEntity) map.get("thrb"));
                }
                THwtjEntity hwtj = (THwtjEntity) map.get("hwtj");
                if (hwtj != null) {
                    THwtjEntity hwtjNew = new THwtjEntity();
                    hwtjNew.setId(task.getBindId());
                    hwtjNew.setCompany(hwtj.getCompany());//公司名称
                    hwtjNew.setSyfzs(hwtj.getSyfzs());
                    hwtjNew.setFzsze(hwtj.getFzsze());
                    hwtjNew.setBscs(hwtj.getBscs());
                    hwtjNew.setJts(hwtj.getJts());
                    hwtjNew.setJfsc(hwtj.getJfsc());//计费时长
                    hwtjNew.setThsc(hwtj.getThsc());//通话时长
                    hwtjNew.setXs(hwtj.getXs());//销售
                    hwtjNew.setOrgname(hwtj.getOrgname());//所属部门
                    hwtjNew.setLine(hwtj.getLine());//线路
                    hwtjNew.setDate(hwtj.getDate());
                    hwtjNew.setBindId(task.getBindId());//回调唯一id
                    hwtjNew.setDl(hwtj.getDl());//代理商
                    hwtjList.add(hwtjNew);
                    String jsonString = JSON.toJSONString(tThrb);
                    zSetVer.producer("hwtj", jsonString);
                }

                start = System.currentTimeMillis();

                TWhbcsEntity tWhbcs = (TWhbcsEntity) map.get("tWhbcs");
                if (tWhbcs != null) {
                    String jsonString = JSON.toJSONString(tWhbcs);
                    zSetVer.producer("tWhbcs", jsonString);
//                    whbcsList.add((TWhbcsEntity) map.get("tWhbcs"));
                }
                TThjlmxEntity thjlmx = (TThjlmxEntity) map.get("thjlmx");
                if (thjlmx != null) {
                    String jsonString = JSON.toJSONString(thjlmx);
                    zSetVer.producer("thjlmx", jsonString);
//                    thjlmxList.add((TThjlmxEntity) map.get("thjlmx"));
                }

                sessionIds.add(vo.getSessionId());
                bakIds.add(task.getId());
                if (map.get("minutes") != null) {
                    String s = String.valueOf(map.get("corporatename"));
                    //是否已存在这个公司数据
                    boolean contains = corporatenames.contains(s);
                    int integer = Integer.valueOf(String.valueOf(map.get("minutes")));
                    logger.info("{}扣除分钟数为:{}", s, integer);
                    if (contains) {
                        for (CorporOV ov : ovs) {
                            //有相同的公司统计分钟数
                            if (StringUtil.equals(ov.getCorporatename(), s)) {
                                int minutes = ov.getMinutes();
                                ov.setMinutes(minutes + integer);
                            }
                        }
                    } else {
                        CorporOV corporOV = new CorporOV();
                        corporOV.setCorporatename(s);
                        corporOV.setMinutes(integer);

                        ovs.add(corporOV);
                        corporatenames.add(s);
                    }
                }
                //中间号处理
                if (map.get("tMiddlenumEntity") != null) {
                    TMiddlenumEntity tMiddlenumEntity = (TMiddlenumEntity) map.get("tMiddlenumEntity");
                    //是否已存在这个公司数据
                    boolean contains = middlenums.contains(tMiddlenumEntity.getMiddlenum());
                    if (contains) {
                        for (TMiddlenumEntity ov : middlenumEntityList) {
                            //有相同的公司统计分钟数
                            if (StringUtil.equals(ov.getMiddlenum(), tMiddlenumEntity.getMiddlenum())) {
                                int minutes = ov.getSycs();
                                ov.setSycs(minutes + 1);
                            }
                        }

                    } else {
                        TMiddlenumEntity o = new TMiddlenumEntity();
                        o.setMiddlenum(tMiddlenumEntity.getMiddlenum());
                        o.setSycs(1);
                        middlenums.add(tMiddlenumEntity.getMiddlenum());
                        middlenumEntityList.add(o);
                    }
                }
                end = System.currentTimeMillis();
                log.info("中间号处理 总耗时：{} ms", end - start);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            long start = System.currentTimeMillis();
//            if (callLogList.size() > 0) {
//                ArrayList<TCallbakLogEntity> collect = callLogList.stream()
//                        .collect(Collectors.collectingAndThen(Collectors
//                                .toCollection(() -> new TreeSet<>(Comparator
//                                        .comparing(s -> s.getBindId()))), ArrayList::new));
//                collect.forEach(System.out::println);
//                callbakLogMapper.insertList(collect);
//            }
            long end = System.currentTimeMillis();
            //  log.info("callLogList 总耗时：{} ms", end - start);

            start = System.currentTimeMillis();
            if (thrbList.size() > 0) {
                ArrayList<TThrbEntity> collect = thrbList.stream()
                        .collect(Collectors.collectingAndThen(Collectors
                                .toCollection(() -> new TreeSet<>(Comparator
                                        .comparing(s -> s.getBindId()))), ArrayList::new));
//                collect.forEach(System.out::println);
                thrbMapper.insertList(collect);
            }
            end = System.currentTimeMillis();
            log.info("thrbList 总耗时：{} ms", end - start);


            start = System.currentTimeMillis();
            if (hwtjList.size() > 0) {
                ArrayList<THwtjEntity> collect = hwtjList.stream()
                        .collect(Collectors.collectingAndThen(Collectors
                                .toCollection(() -> new TreeSet<>(Comparator
                                        .comparing(s -> s.getBindId()))), ArrayList::new));
//                collect.forEach(System.out::println);
                hwtjMapper.insertList(collect);
            }
            end = System.currentTimeMillis();
            log.info("hwtjList 总耗时：{} ms", end - start);

            if (whbcsList.size() > 0) tWhbcsMapper.insertList(whbcsList);

            start = System.currentTimeMillis();
            for (CorporOV ov : ovs) {
                //有相同的公司统计分钟数
                //公司主坐席
                TSeatsEntity gsgl = tSeatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery()
                        .eq(TSeatsEntity::getCorporatename, ov.getCorporatename()
                        ).eq(TSeatsEntity::getGsgl, "1"));
                Integer xhfzs = Integer.valueOf(gsgl.getResidue()) - ov.getMinutes();
                tSeatsMapper.updateByCorporatename(ov.getCorporatename(), xhfzs);
            }
            end = System.currentTimeMillis();
            log.info("有相同的公司统计分钟数总耗时：{} ms", end - start);


            long start2 = System.currentTimeMillis();
            for (TMiddlenumEntity ov : middlenumEntityList) {
                String middlenum = ov.getMiddlenum();
                int minutes = ov.getSycs();
                //中间号  减去剩余分钟数
                TMiddlenumEntity tMiddlenumEntity = tMiddlenumMapper.selectOne(Wrappers.<TMiddlenumEntity>lambdaQuery()
                        .eq(TMiddlenumEntity::getMiddlenum, middlenum));
                tMiddlenumEntity.setHjcs(tMiddlenumEntity.getHjcs() + minutes);
                tMiddlenumEntity.setSycs(tMiddlenumEntity.getSycs() - minutes);
                log.info("中间号实体修改后:{}", tMiddlenumEntity.toString());
                tMiddlenumMapper.updateById(tMiddlenumEntity);
                tXiaohaoMapper.updateByMidd(tMiddlenumEntity);
            }
            long end2 = System.currentTimeMillis();
            log.info("中间号实体修改总耗时：{} ms", end2 - start2);

            long start3 = System.currentTimeMillis();
            logger.info("CallBackTask定时任务  thjlmxList.size() :" + thjlmxList.size());
            if (thjlmxList.size() > 0) {
                thjlmxMapper.deleteListById(thjlmxList);
                thjlmxMapper.insertList(thjlmxList);
            }
            long end3 = System.currentTimeMillis();
            log.info("thjlmxList 总耗时：{} ms", end3 - start3);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    @Override
    public void insertBykeys(String key, Set<String> order) {
        if (StringUtil.equals("thrb", key)) {
            List<TThrbEntity> list = new ArrayList<>();
            for (String s : order) {
                JSONObject jsonObject = JSONObject.parseObject(s);
                TThrbEntity javaObject = JSON.toJavaObject(jsonObject, TThrbEntity.class);
                list.add(javaObject);
            }
            System.out.println("thrb   入库 条数：" + list.size());
            thrbMapper.insertList(list);
        } else if (StringUtil.equals("hwtj", key)) {
            List<THwtjEntity> list = new ArrayList<>();
            for (String s : order) {
                JSONObject jsonObject = JSONObject.parseObject(s);
                THwtjEntity javaObject = JSON.toJavaObject(jsonObject, THwtjEntity.class);
//                System.out.println("hwtj   javaObject.toString()：" + javaObject.toString());
//                System.out.println("hwtj  jsonObject：" + jsonObject.toString());
                list.add(javaObject);
            }
            System.out.println("hwtj   入库 条数：" + list.size());
            hwtjMapper.insertList(list);
        } else if (StringUtil.equals("thjlmx", key)) {//通话记录明细
            List<TThjlmxEntity> list = new ArrayList<>();
            for (String s : order) {
                JSONObject jsonObject = JSONObject.parseObject(s);
                TThjlmxEntity javaObject = JSON.toJavaObject(jsonObject, TThjlmxEntity.class);
                list.add(javaObject);
            }
            System.out.println("thjlmx   入库 条数：" + list.size());
            if (list.size() > 0) {
                thjlmxMapper.deleteListById(list);
                thjlmxMapper.insertList(list);
            }
        } else if (StringUtil.equals("tWhbcs", key)) { //未回拨数据
            List<TWhbcsEntity> list = new ArrayList<>();
            for (String s : order) {
                JSONObject jsonObject = JSONObject.parseObject(s);
                TWhbcsEntity javaObject = JSON.toJavaObject(jsonObject, TWhbcsEntity.class);
                list.add(javaObject);
            }
            if (list.size() > 0) tWhbcsMapper.insertList(list);
        } else if (StringUtil.equals("tMiddlenum", key)) {
            List<TMiddlenumEntity> list = new ArrayList<>();
            List<String> middlenums = new ArrayList<>();
            for (String s : order) {
                JSONObject jsonObject = JSONObject.parseObject(s);
                TMiddlenumEntity tMiddlenumEntity = JSON.toJavaObject(jsonObject, TMiddlenumEntity.class);
                //是否已存在这个中间号数据
                boolean contains = middlenums.contains(tMiddlenumEntity.getMiddlenum());
                if (contains) {
                    for (TMiddlenumEntity ov : list) {
                        //有相同的公司统计分钟数
                        if (StringUtil.equals(ov.getMiddlenum(), tMiddlenumEntity.getMiddlenum())) {
                            int minutes = ov.getSycs();
                            ov.setSycs(minutes + 1);
                        }
                    }
                } else {
                    TMiddlenumEntity o = new TMiddlenumEntity();
                    o.setMiddlenum(tMiddlenumEntity.getMiddlenum());
                    o.setSycs(1);
                    middlenums.add(tMiddlenumEntity.getMiddlenum());
                    list.add(o);
                }
            }
            for (TMiddlenumEntity ov : list) {
                String middlenum = ov.getMiddlenum();
                int minutes = ov.getSycs();
                //中间号  减去剩余分钟数
                TMiddlenumEntity tMiddlenumEntity = tMiddlenumMapper.selectOne(Wrappers.<TMiddlenumEntity>lambdaQuery()
                        .eq(TMiddlenumEntity::getMiddlenum, middlenum));
                tMiddlenumEntity.setHjcs(tMiddlenumEntity.getHjcs() + minutes);
                tMiddlenumEntity.setSycs(tMiddlenumEntity.getSycs() - minutes);
//                log.info("中间号实体修改后:{}", tMiddlenumEntity.toString());
                tMiddlenumMapper.updateById(tMiddlenumEntity);
                tXiaohaoMapper.updateByMidd(tMiddlenumEntity);
            }
        } else if (StringUtil.equals("hwtjFzs", key)) {
            List<THwtjFzsEntity> list = new ArrayList<>();
            List<String> corporatenames = new ArrayList<>();
            for (String s : order) {
                JSONObject jsonObject = JSONObject.parseObject(s);
                THwtjFzsEntity fzs = JSON.toJavaObject(jsonObject, THwtjFzsEntity.class);
                System.out.println("hwtj   javaObject.toString()：" + fzs.toString());
                System.out.println("hwtj  jsonObject：" + jsonObject.toString());
                Integer minutes = fzs.getSyfzs();
                if (fzs.getSyfzs() != null) {
                    String company = fzs.getCompany();
                    //是否已存在这个公司数据
                    boolean contains = corporatenames.contains(company);
                    int integer = Integer.valueOf(minutes);
                    logger.info("{}扣除分钟数为:{}", company, integer);
                    if (contains) {
                        for (THwtjFzsEntity ov : list) {
                            //有相同的公司统计分钟数
                            if (StringUtil.equals(ov.getCompany(), company)) {
                                Integer syfzs = ov.getSyfzs();
                                ov.setSyfzs(syfzs + integer);
                            }
                        }
                    } else {
                        THwtjFzsEntity tHwtjFzs = new THwtjFzsEntity();
                        THwtjFzsEntity fzsEntity = tHwtjFzsMapper.selectOne(Wrappers.<THwtjFzsEntity>lambdaQuery().eq(THwtjFzsEntity::getCompany, company));
                        if (fzsEntity != null) {
                            tHwtjFzs.setFzsze(fzsEntity.getFzsze());
                        } else {
                            tHwtjFzs.setFzsze(0);
                        }
                        tHwtjFzs.setCompany(company);
                        tHwtjFzs.setSyfzs(minutes);

                        list.add(tHwtjFzs);
                        corporatenames.add(s);
                    }
                }
            }
            for (THwtjFzsEntity ov : list) {
                //有相同的公司统计分钟数
                //公司主坐席
                TSeatsEntity gsgl = tSeatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery()
                        .eq(TSeatsEntity::getCorporatename, ov.getCompany()
                        ).eq(TSeatsEntity::getGsgl, "1"));
                Integer xhfzs = Integer.valueOf(gsgl.getResidue()) - ov.getSyfzs();

                ov.setSyfzs(xhfzs);
                tSeatsMapper.updateByCorporatename(ov.getCompany(), xhfzs);
            }

            System.out.println("hwtjFzs   入库 条数：" + list.size());

            //统计分钟数入库
            if (list.size() > 0) {
                tHwtjFzsMapper.deleteListByCompany(list);
                tHwtjFzsMapper.insertList(list);
            }
        } else if (StringUtil.equals("callBackTask", key)) {
            List<TCallbakEntity> list = new ArrayList<>();
            for (String s : order) {
                JSONObject jsonObject = JSONObject.parseObject(s);
                TCallbakEntity javaObject = JSON.toJavaObject(jsonObject, TCallbakEntity.class);
//                System.out.println("callBackTask   javaObject.toString()：" + javaObject.toString());
//                System.out.println("callBackTask  jsonObject：" + jsonObject.toString());
                list.add(javaObject);
            }
            System.out.println("callBackTask   入库 条数：" + list.size());
            if (list.size() > 0) {
                callbakMapper.insertList(list);
            }
        } else if (StringUtil.equals("ovsAndMiddlenum", key)) {
            for (String s : order) {
                JSONObject jsonObject = JSONObject.parseObject(s);
                OvsAndMiddlenum map = JSON.toJavaObject(jsonObject, OvsAndMiddlenum.class);
                List<CorporOV> ovs = map.getCov();
                List<TMiddlenumEntity> middlenumEntityList = map.getMiddlenum();

                for (CorporOV ov : ovs) {
                    //有相同的公司统计分钟数
                    //公司主坐席
                    TSeatsEntity gsgl = tSeatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery()
                            .eq(TSeatsEntity::getCorporatename, ov.getCorporatename()
                            ).eq(TSeatsEntity::getGsgl, "1"));
                    Integer xhfzs = Integer.valueOf(gsgl.getResidue()) - ov.getMinutes();
                    tSeatsMapper.updateByCorporatename(ov.getCorporatename(), xhfzs);
                }



                long start2 = System.currentTimeMillis();
                for (TMiddlenumEntity ov : middlenumEntityList) {
                    String middlenum = ov.getMiddlenum();
                    int minutes = ov.getSycs();
                    //中间号  减去剩余分钟数
                    TMiddlenumEntity tMiddlenumEntity = tMiddlenumMapper.selectOne(Wrappers.<TMiddlenumEntity>lambdaQuery()
                            .eq(TMiddlenumEntity::getMiddlenum, middlenum));
                    tMiddlenumEntity.setHjcs(tMiddlenumEntity.getHjcs() + minutes);
                    tMiddlenumEntity.setSycs(tMiddlenumEntity.getSycs() - minutes);
                    log.info("中间号实体修改后:{}", tMiddlenumEntity.toString());
                    tMiddlenumMapper.updateById(tMiddlenumEntity);
                    tXiaohaoMapper.updateByMidd(tMiddlenumEntity);
                }




//                List<THwtjFzsEntity> list = new ArrayList<>();
//                List<String> corporatenames = new ArrayList<>();
//                for (CorporOV ov : ovs) {
//                    THwtjFzsEntity fzs = tHwtjFzsMapper.selectOne(Wrappers.<THwtjFzsEntity>lambdaQuery().eq(THwtjFzsEntity::getCompany, ov.getCorporatename()));
//                    System.out.println("hwtj   javaObject.toString()：" + fzs.toString());
//                    System.out.println("hwtj  jsonObject：" + jsonObject.toString());
//                    Integer minutes = ov.getMinutes();//扣除分钟数据
//                    if (fzs.getSyfzs() != null) {
//                        String company = fzs.getCompany();
//                        //是否已存在这个公司数据
//                        boolean contains = corporatenames.contains(company);
//                        int integer = Integer.valueOf(minutes);
//                        logger.info("{}扣除分钟数为:{}", company, integer);
//                        if (contains) {
//                            for (THwtjFzsEntity fzd : list) {
//                                //有相同的公司统计分钟数
//                                if (StringUtil.equals(fzd.getCompany(), company)) {
//                                    Integer syfzs = fzd.getSyfzs();
//                                    fzd.setSyfzs(syfzs - integer);
//                                }
//                            }
//                        } else {
//                            THwtjFzsEntity tHwtjFzs = new THwtjFzsEntity();
//                            if (fzs != null) {
//                                tHwtjFzs.setFzsze(fzs.getFzsze());
//                                Integer syfzs = fzs.getSyfzs();
//                                tHwtjFzs.setSyfzs(syfzs - integer);
//                            } else {
//                                tHwtjFzs.setFzsze(0);
//                            }
//                            tHwtjFzs.setCompany(company);
////                            tHwtjFzs.setSyfzs(minutes);
//
//                            list.add(tHwtjFzs);
//                            corporatenames.add(s);
//                        }
//                    }
//                }
//                for (THwtjFzsEntity ov : list) {
//                    //有相同的公司统计分钟数
//                    //公司主坐席
////                    TSeatsEntity gsgl = tSeatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery()
////                            .eq(TSeatsEntity::getCorporatename, ov.getCompany()
////                            ).eq(TSeatsEntity::getGsgl, "1"));
////                    Integer xhfzs = Integer.valueOf(gsgl.getResidue()) - ov.getSyfzs();
//
////                    ov.setSyfzs(xhfzs);
//                    tSeatsMapper.updateByCorporatename(ov.getCompany(), ov.getSyfzs());
//                }
//
//                System.out.println("hwtjFzs   入库 条数：" + list.size());
//
//                //统计分钟数入库
//                if (list.size() > 0) {
//                    tHwtjFzsMapper.deleteListByCompany(list);
//                    tHwtjFzsMapper.insertList(list);
//                }
//
//                List<TMiddlenumEntity> middlenumEntityList = map.getMiddlenum();
//
//                List<TMiddlenumEntity> listMidd = new ArrayList<>();
//                List<String> middlenums = new ArrayList<>();
//                for (TMiddlenumEntity tMiddlenumEntity : middlenumEntityList) {
//                    //是否已存在这个中间号数据
//                    boolean contains = middlenums.contains(tMiddlenumEntity.getMiddlenum());
//                    if (contains) {
//                        for (TMiddlenumEntity ov : listMidd) {
//                            //有相同的公司统计分钟数
//                            if (StringUtil.equals(ov.getMiddlenum(), tMiddlenumEntity.getMiddlenum())) {
//                                int minutes = ov.getSycs();
//                                ov.setSycs(minutes + ov.getSycs());
//                            }
//                        }
//                    } else {
//                        TMiddlenumEntity o = new TMiddlenumEntity();
//                        o.setMiddlenum(tMiddlenumEntity.getMiddlenum());
//                        o.setSycs(tMiddlenumEntity.getSycs());
//                        //没有处理的中间叫加入集合
//                        middlenums.add(tMiddlenumEntity.getMiddlenum());
//                        listMidd.add(o);
//                    }
//                }
//                for (TMiddlenumEntity ov : listMidd) {
//                    String middlenum = ov.getMiddlenum();
//                    int minutes = ov.getSycs();
//                    //中间号  减去剩余分钟数
//                    TMiddlenumEntity tMiddlenumEntity = tMiddlenumMapper.selectOne(Wrappers.<TMiddlenumEntity>lambdaQuery()
//                            .eq(TMiddlenumEntity::getMiddlenum, middlenum));
//                    tMiddlenumEntity.setHjcs(tMiddlenumEntity.getHjcs() + minutes);
//                    tMiddlenumEntity.setSycs(tMiddlenumEntity.getSycs() - minutes);
////                log.info("中间号实体修改后:{}", tMiddlenumEntity.toString());
//                    tMiddlenumMapper.updateById(tMiddlenumEntity);
//                    tXiaohaoMapper.updateByMidd(tMiddlenumEntity);
//                }
            }
        }

    }


}


//
//    List<String> sessionIds = new ArrayList<>();
//    List<String> bakIds = new ArrayList<>();
//    List<String> corporatenames = new ArrayList<>();
//    List<CorporOV> ovs = new ArrayList<>();
//    List<TCallbakLogEntity> callLogList = new ArrayList<>();
//    List<TThrbEntity> thrbList = new ArrayList<>();
//    List<THwtjEntity> hwtjList = new ArrayList<>();
//    List<TWhbcsEntity> whbcsList = new ArrayList<>();
//    List<TMiddlenumEntity> middlenumEntityList = new ArrayList<>();
//    List<String> middlenums = new ArrayList<>();
//
//		for(TCallbakTaskEntity task : tCallbakTaskEntities) {
//                CallbackEntityVo1 vo = new CallbackEntityVo1();
//                vo.setSessionId(task.getBindId());//绑定 ID
//                vo.setBindNbr(task.getZjh());//中间号
//                vo.setCallerNbr(task.getZjhm());//主叫号码
//                vo.setCalleeNbr(task.getBjhm());//被叫号码
//                vo.setStartTime(task.getStartTime());
//                vo.setEndTime(task.getEndTime());
//                vo.setCallOutStartTime(task.getCallOutStartTime());
//                vo.setDuration(task.getDuration());
//                vo.setRecordUrl(task.getRecordUrl());//录音地址
//                vo.setStatus(task.getStatus());
//                vo.setUlFailReason(task.getUlFailReason());
//
//                try {
//                Map<String, Object> map = tCallbakTaskService.notifyurl1(vo);
//        callLogList.add((TCallbakLogEntity)map.get("tCallbakLog"));
//        thrbList.add((TThrbEntity)map.get("thrb"));
//        hwtjList.add((THwtjEntity)map.get("hwtj"));
//        whbcsList.add((TWhbcsEntity)map.get("tWhbcs"));
//
//
//        sessionIds.add(vo.getSessionId());
//        bakIds.add(task.getId());
//        if (map.get("minutes") != null) {
//        String s = String.valueOf(map.get("corporatename"));
//        //是否已存在这个公司数据
//        boolean contains = corporatenames.contains(s);
//        int integer = Integer.valueOf(String.valueOf(map.get("minutes")));
//        logger.info("{}扣除分钟数为:{}",s, integer);
//        if (contains) {
//        for (CorporOV ov : ovs) {
//        //有相同的公司统计分钟数
//        if (StringUtil.equals(ov.getCorporatename(),s)) {
//        int minutes = ov.getMinutes();
//        ov.setMinutes(minutes+integer);
//        }
//        }
//
//        } else {
//        CorporOV corporOV = new CorporOV();
//        corporOV.setCorporatename(s);
//        corporOV.setMinutes(integer);
//
//        ovs.add(corporOV);
//        corporatenames.add(s);
//        }
//        }
//        //中间号处理
//        if (map.get("tMiddlenumEntity") != null) {
//        TMiddlenumEntity tMiddlenumEntity = (TMiddlenumEntity) map.get("tMiddlenumEntity");
//        //是否已存在这个公司数据
//        boolean contains = middlenums.contains(tMiddlenumEntity.getMiddlenum());
//        if (contains) {
//        for (TMiddlenumEntity ov : middlenumEntityList) {
//        //有相同的公司统计分钟数
//        if (StringUtil.equals(ov.getMiddlenum(),tMiddlenumEntity.getMiddlenum())) {
//        int minutes = ov.getSycs();
//        ov.setSycs(minutes+1);
//        }
//        }
//
//        } else {
//        TMiddlenumEntity o = new TMiddlenumEntity();
//        o.setMiddlenum(tMiddlenumEntity.getMiddlenum());
//        o.setSycs(1);
//        middlenums.add(tMiddlenumEntity.getMiddlenum());
//        middlenumEntityList.add(o);
//        }
//        }
//
//        }catch (Exception e){
//        e.printStackTrace();
//        logger.info("Exeception: {}", e.getMessage());
//        }
//
//        }
//        try {
//        if (sessionIds.size() >0){
//
//        }
//
//        if (callLogList.size()>0) callbakLogMapper.insertList(callLogList);
//        if (thrbList.size()>0) thrbMapper.insertList(thrbList);
//        if (hwtjList.size()>0) hwtjMapper.insertList(hwtjList);
//        if (whbcsList.size()>0) tWhbcsMapper.insertList(whbcsList);
//
//        for (CorporOV ov : ovs) {
//        //有相同的公司统计分钟数
//        //公司主坐席
//        TSeatsEntity gsgl = tSeatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery()
//        .eq(TSeatsEntity::getCorporatename,ov.getCorporatename()
//        ).eq(TSeatsEntity::getGsgl, "1"));
//        Integer xhfzs = Integer.valueOf(gsgl.getResidue()) - ov.getMinutes();
//        tSeatsMapper.updateByCorporatename(ov.getCorporatename(), xhfzs);
//        }
//
//        for (TMiddlenumEntity ov : middlenumEntityList) {
//        String middlenum = ov.getMiddlenum();
//        int minutes = ov.getSycs();
//        //中间号  减去剩余分钟数
//        TMiddlenumEntity tMiddlenumEntity = tMiddlenumMapper.selectOne(Wrappers.<TMiddlenumEntity>lambdaQuery()
//        .eq(TMiddlenumEntity::getMiddlenum, middlenum));
//        tMiddlenumEntity.setHjcs(tMiddlenumEntity.getHjcs() + minutes);
//        tMiddlenumEntity.setSycs(tMiddlenumEntity.getSycs() - minutes);
//        //  log.info("中间号实体修改后:{}", tMiddlenumEntity.toString());
//        tMiddlenumMapper.updateById(tMiddlenumEntity);
//
//        tXiaohaoMapper.updateByMidd(tMiddlenumEntity);
//        }
//        } catch (Exception e) {
//        e.printStackTrace();
